import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col, Form, Spin, Modal} from 'antd';
import TextInput from 'Shared/components/TextInput';
import routes from './routes';
import {newOrUpdateValidator} from './validator';
import Return from '../common/Return';
import {injectIntl, language} from './intl';
import {FORM_OPTIONS, FORM_ROW_OPTIONS} from '../constants';
import {claimReasonKind, claimReasonRejectKind, claimReasonType, claimReasonRefuseKind} from '../Enum';
import WrappedSelect from '../common/WrappedSelect';

const claimReasonRejectKindList = claimReasonRejectKind.toList(language);
const claimReasonRefuseKindList = claimReasonRefuseKind.toList(language);
const claimReasonTypeList = claimReasonType.toList(language);
const FormItem = Form.Item;
export class NewPanel extends React.PureComponent {
    state = {
        isValidate: false,
        hasOperat: false,
        loading: false,
        data: [],
    }
    componentDidMount() {
        if(this.props.id)
            this.getDetailInfo();
    }
    componentWillUnmount() {
        this.isunmount = true;
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({loading: true});
        const record = this.props.data.find(d => d.id === id);
        this.setState({
            loading: false,
            data: record,
        });
    }
    onChange = (value, name) => {
        const data = Object.assign({}, this.state.data, {
            [name]: value
        });
        this.setState({
            data,
            hasOperat: true
        });
    }
    onChangeType = (value, name) => {
        const data = Object.assign({}, this.state.data, {
            [name]: value,
            kind: null
        });
        this.setState({
            data,
            hasOperat: true
        });
    }
    onSubmit = () => {
        this.setState({
            loading: true
        });
        const data = this.state.data;
        const isValid = newOrUpdateValidator(data);
        if(!isValid) {
            this.setState({
                isValidate: true,
                loading: false
            });
            return;
        }
        this.props.onSubmit(data)
            .then(res => {
                if(res.success)
                    this.props.successReturn();
                if(!this.isunmount)
                    this.setState({loading: false});
            });
    }
    onClickReturnBtn = () => {
        this.props.onCancel();
    }
    render() {
        const {data, isValidate} = this.state;
        const {formatMessage} = this.props.intl;
        return (
            <div>
                <Spin spinning={this.state.loading}>
                    <Form className="form-standard">
                        <Card title={formatMessage({
                            id: 'addPanel.card.title',
                            defaultMessage: '基本信息'
                        })}>
                            <Row>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.data.code',
                                        defaultMessage: '编号'
                                    })}{...FORM_OPTIONS.item} validateStatus={isValidate && !data.code ? 'error' : null} required >
                                        {data.id ? data.code : <TextInput
                                            name="code"
                                            value={data.code}
                                            onBlur={this.onChange}/>}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'addPanel.data.type',
                                            defaultMessage: '原因类型'
                                        })}
                                        {...FORM_OPTIONS.item}
                                        required>
                                        <WrappedSelect
                                            name="type"
                                            value={data.type}
                                            options={claimReasonTypeList}
                                            onChange={this.onChangeType} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'addPanel.data.kind',
                                            defaultMessage: '原因类别'
                                        })}
                                        {...FORM_OPTIONS.item}
                                        required>
                                        <WrappedSelect
                                            name="kind"
                                            value={data.kind}
                                            options={data.type === claimReasonType.拒绝 ? claimReasonRejectKindList : claimReasonRefuseKindList}
                                            onChange={this.onChange} />
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row className="row-margin">
                                <Col>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.data.description',
                                        defaultMessage: '原因描述'
                                    })}{...FORM_ROW_OPTIONS.item} required><TextInput
                                            name="description"
                                            type="textarea"
                                            value={data.description}
                                            onBlur={this.onChange}/></FormItem>
                                </Col>
                            </Row>
                            <Row className="row-margin">
                                <Col>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.data.remark',
                                        defaultMessage: '备注'
                                    })}{...FORM_ROW_OPTIONS.item}><TextInput
                                            name="remark"
                                            type="textarea"
                                            value={data.remark}
                                            onBlur={this.onChange}/></FormItem>
                                </Col>
                            </Row>
                        </Card>
                    </Form>
                    <Card>
                        <Row>
                            <Col className="col-align-right">
                                <Button key="audit"
                                    onClick={this.onSubmit}
                                    type="primary"
                                    loading={this.state.loading}>
                                    {formatMessage({
                                        id: 'addPanel.btn.submit',
                                        defaultMessage: '保存'
                                    })}</Button>
                            </Col>
                        </Row>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Return
                        onConfirm={this.onClickReturnBtn}
                        disabled={this.state.loading}
                        showWarning={this.state.hasOperat} />
                </div>
            </div>
        );
    }
}

NewPanel.propTypes = {
    data: PropTypes.array.isRequired,
    successReturn: PropTypes.func.isRequired,
    onCancel: PropTypes.func.isRequired,
    onSubmit: PropTypes.func.isRequired,
    intl: PropTypes.object,
};

import {connect} from 'react-redux';
import {tableSearch, onSubmit} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const mapStateToProps = state => ({
    data: getData(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    onSubmit: data => dispatch(onSubmit(data)),
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(NewPanel));
